package com.example.crawler.component;

import com.example.crawler.entity.SchoolLineEntity;
import com.example.crawler.mapper.SchoolLineMapper;
import com.xiaoleilu.hutool.http.Header;
import com.xiaoleilu.hutool.http.HttpRequest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * com.example.crawler.com
 *
 * @author 白雨浓
 * date: 17-10-27 下午3:05
 * content:
 */
@Component
public class SchoolLineComponent {

    private static final String USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0";
    private static final String COOKIE = "UM_distinctid=15f620b167d0-0f5b9099fc89a2-72236751-100200-15f620b167e297; CNZZDATA1255363830=60405890-1509173276-null%7C1510669092; Hm_lvt_fd9bf00761167d2e9689ba3d37779881=1512961318,1514610795; PHPSESSID=gst45117mcdmukr5j10m9gq4b5; Hm_lpvt_fd9bf00761167d2e9689ba3d37779881=1514614505; w_upp=c4c0C8Jxjn7W8%2FSB0LhVb4jOMQ0Om6Z%2BxyNNUb8XoygpwUBZHujFtg-e4d0f6Mtrh5rniMnkVtV1V%2F8i%2FSBEM1mPoPmrc288Kzct1Vhp%2B2WKBGNnhY-6a6191l4uH%2Flw%2FU8DCGwtm81tzzarIdsgaiQ4hQ";

    private final SchoolLineMapper mapper;

    @Autowired
    public SchoolLineComponent(SchoolLineMapper mapper) {
        this.mapper = mapper;
    }

    public void start() {
        int page = 1;
        int batch = 2;
        int year = 2014;

        while (true) {
            batch = 2;
            while (true) {
                page = 1;
                while (true) {
                    String url = "http://www.gaokaoq.com/score/index.html?type=2&city=17&collegeCity=0&batch=" + batch + "&year=" + year + "&q=&p=" + page;
                    String html = HttpRequest.get(url)
                            .header(Header.USER_AGENT, USER_AGENT)
                            .header(Header.COOKIE, COOKIE)
                            .execute().body();

                    Elements schoolLineElements = Jsoup.parse(html).select("div.school_lines"); //带有href属性的a元素
                    String schoolLines = schoolLineElements.html();
                    Elements schoolElements = Jsoup.parse(schoolLines).select("tr");

                    List<SchoolLineEntity> schoolList = new ArrayList<>();
                    for (Element school : schoolElements) {
                        String[] ss = school.text().split(" ");
                        if (ss[0].equals("院校名称")) continue;
                        if (ss.length < 5) continue;
                        schoolList.add(new SchoolLineEntity(ss[0], ss[1], ss[2], ss[3], ss[4], ss[5], ss[6], ss[7], ss[8]));
                    }

                    if (schoolList.size() == 0) break;
                    //dao(schoolList);
                    mapper.insertList(schoolList);
                    System.out.println(year + ":" + batch + ">> 第:" + page + "页，插入:" + schoolList.size() + "行");
                    page++;

                    if (Jsoup.parse(html).select("a.next").size() == 0)
                        break;
                }
                batch++;
                if (batch == 8) break;
            }
            year++;
            if (year == 2017) break;
        }
    }
}
